package com.topc.participant.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.topc.base.exception.Asserts;
import com.topc.participant.model.po.CourseUserMid;
import com.topc.participant.mapper.CourseUserMidMapper;
import com.topc.participant.service.ICourseUserMidService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 用户课程关系表 服务实现类
 * </p>
 *
 * @author Gogoing
 */
@Slf4j
@Service
public class CourseUserMidServiceImpl extends ServiceImpl<CourseUserMidMapper, CourseUserMid> implements ICourseUserMidService {

    @Override
    public void removeInfo(Long courseId, String userNumber) {
        LambdaQueryWrapper<CourseUserMid> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CourseUserMid::getCourseId, courseId)
                .eq(CourseUserMid::getUserNumber, userNumber);
        boolean remove = this.remove(queryWrapper);
        if (!remove){
            Asserts.fail("删除失败");
        }
    }

    @Override
    public void saveInfo(Long courseId, String userNumber) {
        CourseUserMid courseUserMid = new CourseUserMid();
        courseUserMid.setCourseId(courseId);
        courseUserMid.setUserNumber(userNumber);
        boolean save = this.save(courseUserMid);
        if (!save){
            Asserts.fail("添加失败");
        }
    }

    @Override
    public List<Long> getCourseListByNumber(String userNumber) {
        System.out.println(userNumber);
        LambdaQueryWrapper<CourseUserMid> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(CourseUserMid::getCourseId)
        .eq(CourseUserMid::getUserNumber, userNumber);
        List<CourseUserMid> courseList = this.list(queryWrapper);
        List<Long> courseIds = new ArrayList<>();
        courseIds.add(0L);
        for (CourseUserMid courseUserMid : courseList){
            courseIds.add(courseUserMid.getCourseId());
        }
        System.out.println(userNumber+"该用户所有课程id:"+courseIds);
        return courseIds;
    }

    @Override
    public int searchUserCount(Long courseId, String userNumber) {
        LambdaQueryWrapper<CourseUserMid> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(CourseUserMid::getId)
                .eq(CourseUserMid::getCourseId, courseId)
                .eq(CourseUserMid::getUserNumber, userNumber);
        return this.count(queryWrapper);
    }
}
